package com.laiketui.common.mapper;

import com.laiketui.domain.plugin.group.PtGroupProductModel;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @author Trick
 */
public interface PtGroupProductModelMapper extends BaseMapper<PtGroupProductModel> {
    @Override
    List<Map<String, Object>> selectDynamic(Map<String, Object> map) throws LaiKeAPIException;

    @Override
    int countDynamic(Map<String, Object> map) throws LaiKeAPIException;


    /**
     * 获取店铺自己的拼团商品id
     *
     * @param storeId -
     * @param mchId   -
     * @param formId  -
     * @return List
     * @author Trick
     * @date 2021/4/25 17:56
     */
    @Select("select attr_id from lkt_pt_group_product where store_id = #{storeId} and mch_id = #{mchId} and platform_activities_id = #{formId} " +
            " and audit_status in (1,2)")
    List<Integer> selectMchGoodsId(int storeId, int mchId, int formId);


    /**
     * 拼团首页数据(平台)
     *
     * @param map -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/2/20 12:08
     */
    List<Map<String, Object>> getGroupProductIndex(Map<String, Object> map) throws LaiKeAPIException;


    /**
     * 获取活动库存
     *
     * @param goodsId    -
     * @param platformId -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/10/13 10:58
     */
    @Select("select num from lkt_pt_group_product where product_id = #{goodsId} and platform_activities_id = #{platformId} limit 1")
    Integer getGroupProductNum(int goodsId, int platformId) throws LaiKeAPIException;


    /**
     * 根据活动编号获取平台id
     *
     * @param activityNo -
     * @return Integer
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/5/20 10:30
     */
    @Select("select platform_activities_id from lkt_pt_group_product where activity_no =#{activityNo} limit 1")
    Integer getPlatIdByActivityNo(int activityNo) throws LaiKeAPIException;


    /**
     * 获取拼团商品数据
     *
     * @param storeId    -
     * @param goodsId    -
     * @param activityId -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/2/20 17:43
     */
    @Select("SELECT m.*, c.img AS image, c.price AS market_price, " +
            "(select if(max(num) is null,0,max(num)) num from lkt_configure where pid=#{goodsId} and num>0) num" +
            " FROM ( SELECT attr_id, g.product_id, g.group_level, g.group_data, g.g_status ," +
            " l.product_title AS pro_name, l.content, l.STATUS, l.imgurl,l.mch_id, g.activity_no,g.is_show , g.free_freight FROM lkt_pt_group_product g " +
            "LEFT JOIN lkt_product_list l ON g.product_id = l.id WHERE g.store_id = #{storeId} AND g.product_id = #{goodsId} AND g.is_delete = 0 " +
            "AND g.activity_no = #{activityId} GROUP BY g.activity_no ) m LEFT JOIN lkt_configure c ON m.attr_id = c.id LIMIT 0, 1")
    Map<String, Object> getGroupGoodsInfo(int storeId, int goodsId, int activityId) throws LaiKeAPIException;


    /**
     * 增加拼团活动销量
     *
     * @param storeId    -
     * @param activityNo -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/4/1 17:00
     */
    @Update("update lkt_pt_group_product set sales_volume = sales_volume + 1 where store_id = #{storeId}  and activity_no = #{activityNo}")
    int addVolume(int storeId, int activityNo) throws LaiKeAPIException;


    /**
     * 删除拼团商品信息
     *
     * @param storeId -
     * @param plateId -
     * @return int
     * @author Trick
     * @date 2021/5/19 10:38
     */
    @Update("update lkt_pt_group_product set is_delete = 1 where store_id = #{storeId}  and platform_activities_id = #{plateId}")
    int delGroupGoods(int storeId, int plateId);
}
